package com.yxq.mdt.app.modules.personal.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.auction.bean.SendGoodsNum;
import com.yxq.mdt.app.modules.personal.adapter.DenseBrickHistoryItemListAdapter;
import com.yxq.mdt.app.modules.personal.bean.MainPersonTokenBean;
import com.yxq.mdt.app.modules.personal.bean.MainPersonalImNumber;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.bean.RechargeRecordBean;
import com.yxq.mdt.app.modules.personal.model.MainPersonalModel;
import com.yxq.mdt.app.modules.personal.presenter.IMainPersonalContract;
import com.yxq.mdt.app.modules.personal.presenter.MainPersonalPresenter;
import com.yxq.mdt.app.modules.withdraw.ui.WithDrawActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的蜜钻
 */
public class MainPersonalDenseBrickAc extends BaseActivity<MainPersonalPresenter> implements IMainPersonalContract.View {
    DenseBrickHistoryItemListAdapter rechargeHistoryItemListAdapter;
    @BindView(R.id.sugar_tv_balance)
    TextView sugarTvBalance;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main_personal_dense_brick;
    }

    @Override
    public void initView() {
        rechargeHistoryItemListAdapter = new DenseBrickHistoryItemListAdapter(R.layout.item_recharge_history_layout, null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(rechargeHistoryItemListAdapter);
        rechargeHistoryItemListAdapter.setEmptyView(R.layout.layout_empty_view, recyclerView);
        rechargeHistoryItemListAdapter.isUseEmpty(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getPersonalMyData();
            mPresenter.selectEarningsByPageList("gco.add_time DESC", 1, 20);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.selectEarningsByPageList("gco.add_time DESC", page, 20);
        });
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new MainPersonalPresenter(this, new MainPersonalModel());
        refreshLayout.autoRefresh();
    }

    @OnClick(R.id.sugar_tv_recharge)
    public void onViewClicked() {
        if ("0.00".equals(sugarTvBalance.getText().toString()) || "--".equals(sugarTvBalance.getText().toString())) {
            ToastUtils.showShort("蜜钻为0，不能提现");
            return;
        }
        startActivity(new Intent(mActivity, WithDrawActivity.class));
    }

    @Override
    public void onLoadSuccess(MyPersonalBean myPersonalBean) {
        Hawk.put(SPKey.SP_KEY_USER_MY_INFO, myPersonalBean);
        sugarTvBalance.setText(String.format("%.2f", myPersonalBean.getHoneyDrill()));
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mPresenter.getPersonalMyData();
//        mPresenter.selectEarningsByPageList("gco.add_time DESC", 1, 20);
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadPersonalDataFailed() {

    }

    @Override
    public void onLoadPersonalTokenSuccess(MainPersonTokenBean mainPersonTokenBean) {

    }

    @Override
    public void onLoadPersonalTokenFailed() {

    }

    @Override
    public void onLoadImNumberFailed() {

    }

    @Override
    public void onLoadImNumberSuccess(MainPersonalImNumber mainPersonalImNumber) {

    }

    @Override
    public void onLoadAddSaveFailed() {

    }

    @Override
    public void onLoadAddSaveSuccess(MainPersonalImNumber mainSaveAddImNumber) {

    }

    @Override
    public void onGsGiveFailed() {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    @Override
    public void onGsGiveSuccess(RechargeRecordBean rechargeRecordBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();

        if (page == 1) {
            rechargeHistoryItemListAdapter.setNewData(rechargeRecordBean.getList());
        } else {
            if (rechargeRecordBean.getList() != null && rechargeRecordBean.getList().size() != 0) {
                rechargeHistoryItemListAdapter.addData(rechargeRecordBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        rechargeHistoryItemListAdapter.isUseEmpty(!(rechargeHistoryItemListAdapter.getData().size() > 0));
    }

    @Override
    public void inSelectByOrderSendGoodsNumSuccess(SendGoodsNum sendGoodsNum) {

    }

    @Override
    public void inSelectByOrderSendGoodsNumFailed() {

    }

}
